﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using StoreManager.Models;
using StoreManager.DAO;

namespace StoreManager.Controllers
{
    public class ProductController : Controller
    {
        private StoreManagerContext db = new StoreManagerContext();

        //
        // GET: /Product/

        public ActionResult Index()
        {
            ProductSearchView pro = new ProductSearchView();
            return View(pro);
        }
        [HttpPost]
        public ActionResult Index(ProductSearchView pro)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    pro.SearchResults = ProductDAO.SearchProducts(pro);
                }
                return View(pro);
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("ProductController:" + ex.Message);
                //Redirect to Error page
                return RedirectToAction("Error", "Home");
            }
        }

        //
        // GET: /Product/Details/5

        public ActionResult Details(int id = 0)
        {
            Product product = db.Products.Find(id);
            if (product == null)
            {
                return HttpNotFound();
            }
            return View(product);
        }

        //
        // GET: /Product/Create

        public ActionResult Create()
        {                        
            var packTypes = db.SMCodes.Where(s => s.GroupCode.Contains(SMGroupCodes.PACK_TYPES)).OrderBy(s => s.Code);
            ViewBag.PackType = new SelectList(packTypes, "Code", "Name", "");
            var proTypes = db.SMCodes.Where(s => s.GroupCode.Contains(SMGroupCodes.PRODUCT_TYPES)).OrderBy(s => s.Code);
            ViewBag.Type = new SelectList(proTypes.ToList(), "Code", "Name", "");

            return View();
        }

        //
        // POST: /Product/Create

        [HttpPost]
        public ActionResult Create(Product product)
        {
            if (ModelState.IsValid)
            {
                product.LastModified = System.DateTime.Now;
                product.ModifiedBy = "User";
                product.Created = System.DateTime.Now;
                product.CreatedBy = "User";
                db.Products.Add(product);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            var packTypes = db.SMCodes.Where(s => s.GroupCode.Contains(SMGroupCodes.PACK_TYPES)).OrderBy(s => s.Code);
            ViewBag.PackType = new SelectList(packTypes, "Code", "Name", product.PackType);
            var proTypes = db.SMCodes.Where(s => s.GroupCode.Contains(SMGroupCodes.PRODUCT_TYPES)).OrderBy(s => s.Code);
            ViewBag.Type = new SelectList(proTypes.ToList(), "Code", "Name", product.Type);
            
            return View(product);
        }

        //
        // GET: /Product/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Product product = db.Products.Find(id);
            if (product == null)
            {
                return HttpNotFound();
            }
            var packTypes = db.SMCodes.Where(s => s.GroupCode.Contains(SMGroupCodes.PACK_TYPES)).OrderBy(s => s.Code);
            ViewBag.PackType = new SelectList(packTypes, "Code", "Name", product.PackType);
            var proTypes = db.SMCodes.Where(s => s.GroupCode.Contains(SMGroupCodes.PRODUCT_TYPES)).OrderBy(s => s.Code);
            ViewBag.Type = new SelectList(proTypes.ToList(), "Code", "Name", product.Type);
            return View(product);
        }

        //
        // POST: /Product/Edit/5

        [HttpPost]
        public ActionResult Edit(Product product)
        {
            if (ModelState.IsValid)
            {
                db.Entry(product).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            var packTypes = db.SMCodes.Where(s => s.GroupCode.Contains(SMGroupCodes.PACK_TYPES)).OrderBy(s => s.Code);
            ViewBag.PackType = new SelectList(packTypes, "Code", "Name", product.PackType);
            var proTypes = db.SMCodes.Where(s => s.GroupCode.Contains(SMGroupCodes.PRODUCT_TYPES)).OrderBy(s => s.Code);
            ViewBag.Type = new SelectList(proTypes.ToList(), "Code", "Name", product.Type);

            return View(product);
        }

        //
        // GET: /Product/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Product product = db.Products.Find(id);
            if (product == null)
            {
                return HttpNotFound();
            }
            return View(product);
        }

        //
        // POST: /Product/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Product product = db.Products.Find(id);
            db.Products.Remove(product);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}